<?php
//员工父类
abstract class module_public_member{

	function __construct(){
		global $act;

		$this->set_lang();
		$this->$act();
	}

	//提取列表
	protected function index(){
		//todo
	}

	//编辑
	protected function edit(){
		global $Json;
		global $tp;
		global $Db;
		global $Base;
		global $priv_arr;
		global $company;
		
		$id = intval($_REQUEST['id']);  //当前操作的company账号id
		$p_id = $company['is_admin'] ? 0 : $company['id'];
		
		$row = $Db->query("SELECT * FROM ".$Base->table('company')." WHERE id=$id AND p_id=$p_id","Row");
		// var_dump($row);
		//非法的id，则强制id为0
		if(empty($row)){
			$id = 0;
		}
	
		if(empty($_POST)){
			$priv = $Json->decode($row['privileges']);
			$tp->assign('priv',$priv);
			$tp->assign('row',$row);
		}
		else{
			$res['status'] = 0;
			$privileges = array();	//编辑的company_id的权限
			foreach ($priv_arr as $k => $v){
				$privileges[$k] = $_POST[$k];
				if(empty($_POST[$k])){
					unset($privileges[$k]);
				}
			}
			$data = array(
				'id'=>$id,
				'name'=>Common::sfilter($_POST['name']),
				'passwd'=>Common::sfilter($_POST['passwd']),
				'p_id'=>$p_id,
				'privileges'=>$Json->encode_unescaped_unicode($privileges),
			);
			// var_dump($data);
			if(empty($data['name'])){
				$res['msg'] = '登录账号不能为空';
			}
			else if(empty($data['privileges'])){
				$res['msg'] = '请至少为员工分配一个权限';
			}
			else if($Db->query("select id from ".$Base->table('company')." where name='".$data['name']."' and id!=$id",'Row')){
				$res['msg'] = '登录账号已存在，请换一个';
			}
			else{
				// var_dump($id);
				//编辑
				if($id>0){
					//不编辑密码，unset掉
					if(empty($data['passwd'])){
						unset($data['passwd']);	 
					}
					else{
						$data['passwd'] = Common::encrypt($data['passwd']);
					}	
					// var_dump($data);
					$Db->update($Base->table('company'),$data,array('id'=>$id));
				}
				//添加
				else{
					$data['passwd'] = Common::encrypt($data['passwd']);
					if(empty($_POST['passwd'])){
						$res['msg'] = '请输入登录账号';
					}
					else{
						$data['creat_time'] = date('Y-m-d H:i:s',time());
						$id = $Db->insert($Base->table('company'),$data);
					}
				}
				return $id;
			}
			echo $Json->encode($res);
			exit;
		}
	}

	//屏蔽
	protected function dismember(){
		global $company;
		$p_id = $company['is_admin'] ? 0 : $company['id'];
		$id = intval($_REQUEST['id']);
		$row = $GLOBALS['Db']->query("SELECT * FROM ".$GLOBALS['Base']->table('company')." WHERE id=$id","Row");
		if($p_id){
			$sql = " AND p_id=".$p_id;
		}
		else{
			$sql = " OR p_id=".$id;
		}
		if(empty($row)){
			$res = array('status'=>'0','msg'=>'非法操作');
		}
		else{	
			$is_not=intval($_POST['is_not']);
			// echo "UPDATE ".$GLOBALS['Base']->table('company')." SET is_not = $is_not WHERE id=$id ".$sql."";die;
			$GLOBALS['Db']->query("UPDATE ".$GLOBALS['Base']->table('company')." SET is_not = $is_not WHERE id=$id ".$sql."");
			$res = array('status'=>'1','msg'=>'操作成功');
		}
		echo $GLOBALS['Json']->encode($res);
		exit;
		
	}

	//设置语言变量
	protected function set_lang(){

	}
}

?>